package parts;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;

//封装数据库的连接配置，使用连接池
public class Link {
    Context context;
    Context contextNeeded;
    DataSource ds;
    Connection con=null;
    public Connection create(){
        try {
            context = new InitialContext();
            contextNeeded = (Context) context.lookup("java:comp/env/gxy");
            ds = (DataSource) contextNeeded.lookup("gxy");
            con = ds.getConnection();
        } catch (Exception e){
            System.out.println(e);
        }
        if(con!=null){
            System.out.println("连接成功");
        }else if(con==null){
            System.out.println("连接失败");
        }
        return con;
    }
    public void close(Connection con)throws Exception{
        if(con!=null) {
            con.close();
            System.out.println("关闭连接");
        }
    }
}
